package com.pinguix.games.pong;

import danger.app.Event;
import danger.app.Listener;
import danger.app.Timer;
import danger.ui.Color;
import danger.ui.Font;
import danger.ui.Pen;
import danger.ui.Point;
import danger.ui.ScreenWindow;
import danger.util.DEBUG;

public class PongWindow extends ScreenWindow
{
	private Paddle _p1Paddle;
	private Paddle _p2Paddle;
	private Ball _ball;
	private CollisionManager _collisionMan;

	private int _p1Score = 0;
	private int _p2Score = 0;
	private static final Font _scoreFont = Font.findFont("BortBold12");
	
	private PongAI _p2AI;

	private String _pauseStr = "PAUSED";
	private boolean _pauseGame = false;
	
	//Constructor
	public PongWindow()
	{
		_p1Paddle = new Paddle(new Point(15, 0), this.getBounds());
		_p2Paddle = new Paddle(new Point(getWidth() - Paddle.WIDTH - 15, 0), this.getBounds());
		_ball = new Ball(new Point(50, 50), this);
		_p2AI = new PongAI(_p2Paddle, _ball);
		_p2Paddle.setMaxSpeed(9); //While in development, lets keep it at 9
		
		_collisionMan = new CollisionManager(this, _p1Paddle, _p2Paddle, _ball);
		LEDManager.activate();
	}

	//Paint Function
	public void paint(Pen pen)
	{
		clear(pen);
		pen.setColor(Color.BLACK);

		pen.setFont(_scoreFont);
		_p1Paddle.paint(pen);
		_p2Paddle.paint(pen);
		_ball.paint(pen);

		String scoreStr = (_p1Score + "  -  " + _p2Score);
		pen.drawText(getWidth() / 2 - _scoreFont.getWidth(scoreStr) / 2, 50, scoreStr);
		
		if(_pauseGame)
		{
			pen.drawText(getWidth()/2 - _scoreFont.getWidth(_pauseStr)/2, 100, _pauseStr);
			Pong.pause();
		}
	}

	//Updates all positional variables
	public void update()
	{
		_p2AI.update();
		_p1Paddle.update();
		_p2Paddle.update();
		_ball.update();
		_collisionMan.update();
		this.invalidate();
	}

	public void addPointLeft()
	{
		LEDManager.lightUpLeft();
		_p1Score++;
		_ball.reset();
		DEBUG.p("SCORE: " + _p1Score + " - " + _p2Score);
	}

	public void addPointRight()
	{
		LEDManager.lightUpRight();
		_p2Score++;
		_ball.reset();
		DEBUG.p("SCORE: " + _p1Score + " - " + _p2Score);
	}
	
	//Detects when a widget is held down
	public boolean eventWidgetDown(int widgetKey, Event event)
	{
		switch (widgetKey)
		{
			case Event.DEVICE_NAVIGATION:
				byte v = event.getVerticalCount();
				// positive v == move down
				// negative v == move up
				if(v > 0)
				{
					_p1Paddle.moveDown();
				} 
				else if(v < 0)
				{
					_p1Paddle.moveUp();
				} 
				else break;
				break;
			case Event.DEVICE_ARROW_UP:
			case Event.DEVICE_WHEEL_PAGE_UP:
				_p1Paddle.moveUpConstant();
				break;
			case Event.DEVICE_ARROW_DOWN:
			case Event.DEVICE_WHEEL_PAGE_DOWN:
				_p1Paddle.moveDownConstant();
				break;
		}
		return true;
	}

	//Detects when a widget is released
	public boolean eventWidgetUp(int widgetKey, Event event)
	{
		switch (widgetKey)
		{
			case Event.DEVICE_ARROW_UP:
			case Event.DEVICE_WHEEL_PAGE_UP:
			case Event.DEVICE_ARROW_DOWN:
			case Event.DEVICE_WHEEL_PAGE_DOWN:
			case Event.DEVICE_WHEEL_BUTTON:
				_p1Paddle.stopMoving();
				break;
			case Event.DEVICE_BUTTON_BACK:
			case Event.DEVICE_BUTTON_JUMP:
			  _pauseGame = true;
			  Pong.getInstance().returnToLauncher();
			  break;
			case Event.DEVICE_BUTTON_MENU:
              if(Pong.isPaused())
              {
                  _pauseGame = false;
                  Pong.unpause();
              }
              else
                _pauseGame = true;
				break;
				
			//SOME EASTER EGGS FOR TEMPORARY FUN!! :)
			case Event.DEVICE_BUTTON_CANCEL:
				if(_p2Paddle.getMaxSpeed() == Paddle.DCONSTSPEED)
				{
					_p2Paddle.setMaxSpeed(9);
				} else if(_p2Paddle.getMaxSpeed() == 9)
				{
					_p2Paddle.setMaxSpeed(13);
				} else if(_p2AI.isActive())
				{
					_p2Paddle.setHeight(getBounds().bottom);
					_p2AI.deactivate();
				} else
				{
					_p2AI.activate();
					_p2Paddle.setHeight(Paddle.HEIGHT);
					_p2Paddle.setMaxSpeed(Paddle.DCONSTSPEED);
				}
				break;
			case Event.DEVICE_BUTTON_RIGHT_SHOULDER:
				if(_ball.getSpeed() < 25)
					_ball.setSpeed(_ball.getSpeed() + 3);
				else
					_ball.setSpeed(Ball.DSPEED);
				break;
			case Event.DEVICE_BUTTON_LEFT_SHOULDER:
				_p1Score = 0;
				_p2Score = 0;
				_ball.reset();
				break;
		}
		return true;
	}
}
